package com.fita.admin.service.data.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.fita.admin.dao.CurrencyDao;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.data.CurrencyService;
import com.fita.entity.data.ExchangeRate;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 
 * @author cuteHU
 *
 */
@Service("currencyService")
public class CurrencyServiceImpl extends DataServiceImpl<ExchangeRate> implements CurrencyService{

    @Dao
    private CurrencyDao currencyDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /* (non-Javadoc)
     * @see com.fita.admin.service.data.CurrencyService#listCurrentRate()
     */
    public List<ExchangeRate> listCurrentRate() {
    	
    	List<ExchangeRate> exchangeRates = new ArrayList<ExchangeRate>();
    	
    	Date now = new Date();
    	
    	// 获取所有汇率代码
    	List<String> allCurrencyCodes = currencyDao.listAllCurrencyCode();
    	
    	for (String currencyCode : allCurrencyCodes) {
			// 获取当前生效汇率
    		ExchangeRate currentExchangeRate = currencyDao.loadCurrentRate(currencyCode, now);
    		
    		if (currentExchangeRate != null) {
        		exchangeRates.add(currentExchangeRate);
			}
		}
        return exchangeRates;
    }

    /**
     * @return
     */
    public int count() {
    	
        return currencyDao.count();
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.data.CurrencyService#createCurrency(com.fita.entity.data.ExchangeRate, java.lang.Long)
     */
    public void createCurrency(ExchangeRate exchangeRate,Long id) {
        List<ExchangeRate> eList = currencyDao.listByCode(exchangeRate.getCurrencyCode());
        if (eList.size() > 0) {
            for (ExchangeRate eRate : eList) {
                if (! (eRate.getCurrencyName().equals(exchangeRate.getCurrencyName()))) {
                    throw BusinessException.newInstance("400", "不同货币的货币代码不可重复");
                }
            }
            
        }
        genericDaoSupport.save(exchangeRate.initliaze(id));
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.data.CurrencyService#countSearch(java.lang.Class, com.demo2do.core.persistence.support.QueryFilter)
     */
    public int countSearch(Class<ExchangeRate> clazz, QueryFilter queryFilter) {
        return genericDaoSupport.searchForInt("SELECT COUNT(*) FROM " + ClassUtils.getShortClassName(clazz) + queryFilter.whereSentence(), queryFilter.parameters());
    }

	/*
	 * (non-Javadoc)
	 * @see com.fita.admin.service.data.CurrencyService#load(java.lang.String)
	 */
	public ExchangeRate load(String code) {
		
		return currencyDao.load(code);
	}

	/* (non-Javadoc)
	 * @see com.fita.admin.service.DataServiceImpl#listByPage(java.lang.Class, com.demo2do.core.entity.page.Page)
	 */
	@SuppressWarnings("unchecked")
	public List<ExchangeRate> listByPage(Class<ExchangeRate> clazz, Page page) {
		List<ExchangeRate> result = genericDaoSupport.searchForList("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE 1 = 1", page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
	}

}
